//
//  KKBaseNavController.swift
//  SinaWeiBo
//
//  Created by King.Kong. on 16/5/22.
//  Copyright © 2016年 King.Kong. All rights reserved.
//

import UIKit

class KKBaseNavController: UINavigationController,UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.interactivePopGestureRecognizer?.delegate = self
        
        // 设置背景
//        navigationController?.navigationBar.setBackgroundImage(UIImage(named: "NavBar"), forBarMetrics: .Default)
    }
    
    
    
    
     // 重写push
    override func pushViewController(viewController: UIViewController, animated: Bool) {
        
        var backTitle = "返回"
        
        if childViewControllers.count > 0 {
        
            if childViewControllers.count == 1 {
                backTitle = childViewControllers.first?.title ?? ""
            }
            viewController.navigationItem.leftBarButtonItem =  UIBarButtonItem(imageName: "navigationbar_back_withtext", title: backTitle, target: self, action: #selector(popToFrontVC))
            viewController.hidesBottomBarWhenPushed = true
        }
        
        super.pushViewController(viewController, animated: animated)

    }
    
    
    @objc private func popToFrontVC() {
        
        popViewControllerAnimated(true)
    }
//    
    func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
        
//        if childViewControllers.count > 1 {
//            return true
//        }
//        return false
        return childViewControllers.count > 1
    }
}
